package cn.edu.sdyu.ie.crs.common.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.ToString;

@ApiModel("统一响应消息")
@Getter
@ToString
public class Result<T> {

    @Schema(description = "业务错误码")
    private Integer code;  // 业务错误码
    @Schema(description = "信息描述")
    private String message;  // 信息描述
    @Schema(description = "响应数据")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;  // 响应数据

    private Result(ResultStatus resultStatus, T data) {
        this.code = resultStatus.getCode();
        this.message = resultStatus.getMessage();
        this.data = data;
    }

    public static <Void> Result<Void> success() {
        return new Result<>(ResultStatus.SUCCESS, null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultStatus.SUCCESS, data);
    }

    public static <Void> Result<Void> failure() {
        return new Result<>(ResultStatus.INTERNAL_SERVER_ERROR, null);
    }

    public static <T> Result<T> failure(T data) {
        return new Result<>(ResultStatus.INTERNAL_SERVER_ERROR, data);
    }

}
